﻿// $Id: Traces.cs 10 2008-02-29 15:58:02Z mcartoixa $

/* ***************************************************************************
 *                                                                           *
 * Copyright © 2007 NourY Solutions                                          *
 *                                                                           *
 * This file is part of DataRules.                                           *
 *                                                                           *
 * DataRules is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published     *
 * by the Free Software Foundation; either version 3 of the License, or      *
 * (at your option) any later version.                                       *
 *                                                                           *
 * DataRules is distributed in the hope that it will be useful, but          *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU          *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this program.  If not, see                             *
 * <http://www.gnu.org/licenses/lgpl.html>.                                  *
 *                                                                           *
 *************************************************************************** */

/*! \file
 * \brief Assembly traces.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Threading;

namespace Salamanca.DataRules
{
#if (NET_CF)
    internal class TraceSwitch
    {
    }

    internal enum TraceLevel
    {
    }
#endif


    internal static class Traces
    {
        public static string GetCategory(TraceSwitch sw)
        {
#if (!NET_CF)
            return string.Format(
                CultureInfo.CurrentCulture,
                "[{0:s}] [Thread <0x{1:X4}>] [Trace <{2}>, Level <{3}>]",
                DateTime.Now,
                Thread.CurrentThread.ManagedThreadId,
                sw.DisplayName,
                sw.Level
            );
#else
            return string.Empty;
#endif
        }

        [Conditional("NET_1_0"), Conditional("NET_1_1"), Conditional("NET_2_0"), Conditional("NET_3_0")]
        public static void TraceValidation(TraceSwitch sw, IRule rule, bool validation)
        {
#if (!NET_CF)
            if ((!validation && sw.TraceInfo) || sw.TraceVerbose)
            {
                Trace.WriteLine(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        "Rule {0} validation {1}",
                        rule.GetType(),
                        validation ? "succeded" : "failed"
                    ),
                    Traces.GetCategory(Configuration.Switch)
                );
                if (Configuration.Switch.TraceVerbose)
                {
                    Trace.WriteLine(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "Property name: {0}",
                            rule.PropertyName
                        )
                    );
                    Trace.WriteLine(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "Description: {0}",
                            rule.Description
                        )
                    );
                }
            }
#endif
        }

    }
}
